package script;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import java.io.*;
import java.util.StringTokenizer;
public class login {
    public WebDriver driver;
    @Test
    public void login1() throws IOException, InterruptedException {
        String driverPath = new File("./").getCanonicalPath() + "/src/test/java/driver/chromedriver.exe";
        String driverName = "webdriver.chrome.driver";
        System.setProperty(driverName, driverPath);
        // System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
        driver = new ChromeDriver();
        //driver.get(test_url);
        driver.manage().window().maximize();
       driver.get("http://omp.wangtest.cn/web/user/login");
       driver.findElement(By.linkText("使用账户密码登录")).click();
       driver.findElement(By.id("username")).clear();
       driver.findElement(By.id("username")).sendKeys("admin");
       driver.findElement(By.id("password")).clear();
       driver.findElement(By.id("password")).sendKeys("Paycloud2019");
       driver.findElement(By.className("ant-checkbox-input")).click();
       Thread.sleep(10000);
       //[user-key=paycloud-omp@user_1602319002558; path=/; domain=omp.wangtest.cn, SESSION=a75d0566-9ed2-4781-815a-e48db7703b3a; path=/; domain=omp.wangtest.cn]
       System.out.println("myCookies:"+driver.manage().getCookies());
/*
        File cookieFile = new File("d://login_cookie.txt");
        try {
            cookieFile.delete();
            cookieFile.createNewFile();
            FileWriter fileWriter = new FileWriter(cookieFile);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
            for (Cookie cookie:driver.manage().getCookies()){
                bufferedWriter.write((cookie.getName()+";"+
                        cookie.getValue()+";"+
                        cookie.getDomain()+";"+
                        cookie.getPath()));
                bufferedWriter.newLine();
            }
            bufferedWriter.flush();
            bufferedWriter.close();
            fileWriter.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
*/
        driver.quit();
    }
    @Test
    public void login_readCookie() throws IOException, InterruptedException {
        BufferedReader bufferedReader;
        String driverPath = new File("./").getCanonicalPath() + "/src/test/java/driver/chromedriver.exe";
        String driverName = "webdriver.chrome.driver";
        System.setProperty(driverName, driverPath);
        // System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
        driver = new ChromeDriver();
        //driver.get(test_url);
        driver.manage().window().maximize();
        driver.get("http://omp.wangtest.cn/web/user/login");
        driver.manage().deleteAllCookies();
        try {
            File cookieFile = new File("d://login_cookie.txt");
            FileReader fileReader = new FileReader(cookieFile);
            bufferedReader = new BufferedReader(fileReader);
            String line;
            while ((line = bufferedReader.readLine()) != null){
                StringTokenizer stringTokenizer = new StringTokenizer(line, ";");
                while (stringTokenizer.hasMoreTokens()){
                    String value = stringTokenizer.nextToken();
                    String domain = stringTokenizer.nextToken();
                    String path = stringTokenizer.nextToken();
                    String dt;
                    Cookie cookie = new Cookie(value,domain,path);
                    driver.manage().addCookie(cookie);
                }
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("myCookies-end:"+driver.manage().getCookies());
        driver.get("http://omp.wangtest.cn/web/user/login");
    }
    @Test
    public void login3() throws IOException, InterruptedException {
       String driverPath = new File("./").getCanonicalPath() + "/src/test/java/driver/chromedriver.exe";
        String driverName = "webdriver.chrome.driver";
        System.setProperty(driverName, driverPath);
        // System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
        driver = new ChromeDriver();
        //driver.get(test_url);
        driver.manage().window().maximize();
        driver.get("http://omp.wangtest.cn/web/user/login");
        driver.findElement(By.linkText("使用账户密码登录")).click();
        driver.manage().deleteAllCookies();
        System.out.println("start-add -cookie");
        //Cookie cookie1 = new Cookie("SESSION","e37ef163-0b8e-4bee-b2f7-fec6175d0c1c","omp.wangtest.cn");
        Cookie cookie1 = new Cookie("SESSION","e37ef163-0b8e-4bee-b2f7-fec6175d0c1c","/");
        driver.manage().addCookie(cookie1);
        System.out.println("insert-myCookies:"+driver.manage().getCookies());
        driver.navigate().refresh();
      //  driver.get("http://omp.wangtest.cn/web/user/login");
        System.out.println("------------------------------------------");
        System.out.println("add -cookie-end");
        System.out.println("myCookies-end:"+driver.manage().getCookies());
    }
}